<?php

/**
 * This is the model class for table "categories".
 *
 * The followings are the available columns in table 'categories':
 * @property integer $category_id
 * @property string $name
 * @property string $alias
 * @property string $icon
 * @property string $meta_keywords
 * @property string $meta_description
 * @property string $meta_title
 * @property integer $created
 * @property integer $updated
 * @property float $price_two_week
 * @property float $price_one_month
 * @property float $$price_addition
 *
 * The followings are the available model relations:
 * @property Advertising[] $advertisings
 */
class Category extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Category the static model class
     */
    public static function model($className = __CLASS__) {
	return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
	return '{{ad_categories}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
	// NOTE: you should only define rules for those attributes that
	// will receive user inputs.
	return array(
	    array('name', 'required'),
	    array('alias', 'unique'),
	    array('created, updated', 'numerical', 'integerOnly' => true),
	    array('price_two_week, price_one_month, price_3_month, price_6_month, price_one_year, price_addition, price_e_blast', 'numerical'),
	    array('name, alias, icon, meta_keywords, meta_description, meta_title', 'length', 'max' => 255),
	    // The following rule is used by search().
	    // Please remove those attributes that should not be searched.
	    array('category_id, name, alias, icon, meta_keywords, meta_description, meta_title, created, updated', 'safe', 'on' => 'search'),
	);
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
	// NOTE: you may need to adjust the relation name and the related
	// class name for the relations automatically generated below.
	return array(
	    'advertising' => array(self::HAS_MANY, 'Advertising', 'category_id'),
	);
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
	return array(
	    'category_id' => 'Category',
	    'name' => 'Name',
	    'alias' => 'Alias',
	    'icon' => 'Icon',
	    'meta_keywords' => 'Meta Keywords',
	    'meta_desciption' => 'Meta Desciption',
	    'meta_title' => 'Meta Title',
	    'created' => 'Created',
	    'updated' => 'Updated',
	);
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
	// Warning: Please modify the following code to remove attributes that
	// should not be searched.

	$criteria = new CDbCriteria;

	$criteria->compare('category_id', $this->category_id);
	$criteria->compare('name', $this->name, true);
	$criteria->compare('alias', $this->alias, true);
	$criteria->compare('icon', $this->icon, true);
	$criteria->compare('meta_keywords', $this->meta_keywords, true);
	$criteria->compare('meta_description', $this->meta_description, true);
	$criteria->compare('meta_title', $this->meta_title, true);
	$criteria->compare('created', $this->created);
	$criteria->compare('updated', $this->updated);

	return new CActiveDataProvider($this, array(
	    'criteria' => $criteria,
	));
    }

    /**
     * action before save
     * @return boolean
     */
    public function beforeSave() {
	if (parent::beforeSave()) {
	    if ($this->isNewRecord) {
		$this->updated = $this->created = time();
	    } else {
		$this->updated = time();
	    }

	    //create site alias
	    if ($this->alias == '') {
		$this->alias = StringHelper::encode($this->name, '-', false);
	    } else {
		$this->alias = StringHelper::encode($this->alias, '-', false);
	    }

	    return true;
	}
    }

    public function getIconUrl() {
	return UPLOAD_URL . $this->icon;
    }

    /**
     * get front end url
     * @return string url to category page
     */
    public function getUrl() {
	return url('/advertising/category', array('alias' => $this->alias));
    }

    /**
     * get the list of categories
     * @return type
     */
    public static function getList() {
	$categories = app()->cache->get('category_list_cache');

	if (!$categories) {
	    $categories = Category::model()->findAll(array('order' => 'name'));
	    app()->cache->set('category_list_cache', $categories);
	}
	return $categories;
    }

    /**
     * get price
     * @param string $type price_two_week, price_one_month, price_addition
     */
    public static function getPrice($id, $type = 'price_3_month') {
	return db()->createCommand()->select($type)
			->from('{{ad_categories}}')
			->where('category_id = :catId', array(
			    ':catId' => $id
			))
			->queryScalar();
    }

}

